﻿var cGCusID = 0;
var cGSaleID = 0;
var cGAmcID = 0;
var isWizzard = 0;

$(document).ready(function () {
    var mainContainer = $('#addCustomerWizzard');
    cGCusID = $('input[type=hidden][id*=hidCusGuid]').val();
    clearForm(mainContainer);
    $('.placeholderMenuItem_Clicked').removeClass('placeholderMenuItem_Clicked');
    $('#menuNCustomer').attr('class', 'placeholderMenuItem_Clicked');
});

function allowOnlyNumber(event) {
    if (event.keyCode == 46 || event.keyCode == 8) {
    }
    else {
        if (event.keyCode < 48 || event.keyCode > 57) {
            event.preventDefault();
        }
    }
}

function enableTextbox() {
    $('#txtAMCInvoiceDate').removeAttr('disabled');
    $('#dateEnable').hide();
    $('#dateDisable').show();
}
function disableTextbox() {
    $('#txtAMCInvoiceDate').attr('disabled', 'disabled');
    $('#dateEnable').show();
    $('#dateDisable').hide();
}
//$(window).unload(function () {
//    $(':input', '#formCustomerInfo')
// .not(':button, :submit, :reset, :hidden')
// .val('')
//});

//$(function () {
//    $("#txtAMCRate").keydown(function (event) {
//        if (event.keyCode == 46 || event.keyCode == 8) {
//        }
//        else {
//            if (event.keyCode < 48 || event.keyCode > 57) {
//                event.preventDefault();
//            }
//        }
//    });
//});
function validateForm() {
    $('#addCustomerForm').validate({
        rules: {
            name: "required",
            email: {
                required: true
            }
        }
    });
}
function saveCustomer() {
    var cusGuid = cGCusID;
    var cusID = genDynmID();
    var cusName = $('#txtCusName').val();
//    var cusFname = $('#txtFname').val();
//    var cusLname = $('#txtLname').val();
    var cusMobile = $('#txtMobile').val();
    var cusAltPhone = $('#txtAltPhone').val();
    var cusEmailID = $('#txtEmailID').val();
    var cusAddress1 = $('#txtAddress1').val();
    var cusCity = $('#txtCity').val();
    var cusState = $('#txtState').val();
    var cusLandmark = $('#txtLandmark').val();
    var cusPostCode = $('#txtPostcode').val();
    var insUser = '2712BC15-BABE-4A23-9E3C-A8119695BD38'; //should add cookie value later
    var updUser = '2712BC15-BABE-4A23-9E3C-A8119695BD38'; //should add cookie value later
    var insDt = getCurDate();
    var updDt = getCurDate();

    if (cusName == "") {
        $("#valFname").text("Enter the customer first name");
    }
    else if (cusMobile == "") {
        $("#valFname").text("");
        $('#valMobile').text("Enter the customer mobile number");
    }
    else if (cusEmailID == "") {
        $("#valFname").text("");
        $("#valMobile").text("");
        $('#valEmail').text("Enter the customer email ID");
    }
    else if (cusAddress1 == "") {
        $("#valFname").text("");
        $("#valMobile").text("");
        $("#valEmail").text("");
        $('#valAddress').text("Enter the customer address");
    }
    else {
        var params = { 'cGid': cusGuid, 'cID': cusID, 'cNm': cusName, 'cMob': cusMobile, 'cAltP': cusAltPhone, 'cEmail': cusEmailID, 'cAd1': cusAddress1, 'cLnMrk': cusLandmark, 'cCity': cusCity, 'cState': cusState, 'cPcode': cusPostCode, 'inUsr': insUser, 'upUsr': updUser };
        var containerDIV = $('#addCustomerWizzard');
        var opts = { 'isLoadingGif': true, 'container': containerDIV };
        MakeJAjaxCall("../_services/addCustomer.ashx", params, 'html', fnCusAddSuccess, '', '', opts);
    }
    //        var parentDiv = $('#addCustomerWizzard');
    //        var moduleName = "../../_modules/ucSalesDetails.ascx";
    //        var _params = '&cid=' + cGCusID;
    //        var opts = { 'onsucess': onUCSalesLoaded, 'params': _params };
    //        loadModuler(moduleName, parentDiv, opts);
}
function fnCusAddSuccess(response) {
    if (response == "y") {
        var parentDiv = $('#addCustomerWizzard');
        var moduleName = "../../_modules/ucSalesDetails.ascx";
        var _params = '&cid=' + cGCusID;
        var opts = { 'onsucess': onUCSalesLoaded, 'params': _params };
        loadModuler(moduleName, parentDiv, opts);
    }
    else if (response == "n") {
        
    }
}
function onUCSalesLoaded() {

}
function saveSaleBillData() {
    var sale_guid = cGSaleID;
    var sale_ID = genDynmID();
    var sale_billNo = $('#txtSaleBill').val();
    var sale_billDt = $('#txtBillDate').val();
    var sale_dealer = $('#txtDealer').val();
    //var sale_brand = $('#ddlBrands option:selected').text();
    var sale_product = $('#txtProductName').val();
    //var sale_ton = $('#ddlTon option:selected').text();
    var sale_modalno = $('#txtModalNo').val();
    var sale_compressorno = $('#txtCompressorNo').val();
    var sale_outdoorno = $('#txtOutdoorNo').val();
    var sale_indoorno = $('#txtIndoorNo').val();
    //var sale_modalName = $('#txtModalName').val();
    var sale_productType = $('#ctl02_ddlProdCate option:selected').text();
    var sale_comments = $('#txtAreaComments').val();
    var sale_cusid = cGCusID;
    var insUser = '2712BC15-BABE-4A23-9E3C-A8119695BD38'; //should add cookie value later
    var updUser = '2712BC15-BABE-4A23-9E3C-A8119695BD38'; //should add cookie value later

    if (sale_billNo == "") {
        $('#valBillNo').text("Enter the bill number");
    }
    else if (sale_billDt == "") {
        $('#valBillNo').text("");
        $('#valBillDate').text("Enter the billing date");
    }
    else {
        var params = { 'sGid': sale_guid,
            'sID': sale_ID,
            'sBNo': sale_billNo,
            'sBDt': sale_billDt,
            'sDlr': sale_dealer,
            'sBrnd': '',
            'sProd': sale_product,
            'sProdTyp': sale_productType,
            'sComnts': sale_comments,
            'sCusID': sale_cusid,
            'sInUsr': insUser,
            'sUpUsr': updUser,
            'sMdlNo': sale_modalno,
            'sCompNo': sale_compressorno,
            'sOutdrNo': sale_outdoorno,
            'sIndrNo': sale_indoorno,
            'sMdlNme': ''
        }
        var containerDIV = $('#addCustomerWizzard');
        var opts = { 'isLoadingGif': true, 'container': containerDIV };
        MakeJAjaxCall("../_services/insertSale.ashx", params, 'html', fnSaleAddSuccess, '', '', opts);
    }
//    var parentDiv = $('#addCustomerWizzard');
//    var moduleName = "../../_modules/ucAmcDetails.ascx";
//    var _params = '&cid=' + cGCusID + '&sid=' + cGSaleID;
//    var opts = { 'onsucess': onUCSalesLoaded, 'params': _params };
//    loadModuler(moduleName, parentDiv, opts);
}
function fnSaleAddSuccess(response) {
    if (response == "y") {
        var parentDiv = $('#addCustomerWizzard');
        var moduleName = "../../_modules/ucAmcDetails.ascx";
        var _params = '&cid=' + cGCusID + '&sid=' + cGSaleID;
        var opts = { 'onsucess': onUCSalesLoaded, 'params': _params };
        loadModuler(moduleName, parentDiv, opts);
    }
    else {
        
    }
}
function amcBackClick() {
    var parentDiv = $('#addCustomerWizzard');
    var moduleName = "../../_modules/ucSalesDetails.ascx";
    var _params = '&cid=';
    var opts = { 'onsucess': onUCSalesLoaded, 'params': _params };
    loadModuler(moduleName, parentDiv, opts);
}
function saveAMCDetails() {
    //alert("save amc");
    var amc_guid = $('input[type=hidden][id*=hidAMCGuid]').val();
    var amc_id = $('#txtAmcInvoiceNo').val();
    var amc_invoice = $('#txtAmcInvoiceNo').val();
    var amc_invoiceDt = $('#txtInvoiceDt').val();
    var amc_warranty = $('#txtWarrantyFrom').val();
    var amc_expiry = $('#expiryDate').val();
    var amc_pmode = $('input:radio[name=paymode]:checked').val();
    var d1 = new Date($('#txtWarrantyFrom').val()); // January 1st 2010
    var d2 = new Date($('#expiryDate').val()); // February 1st 2010
    var diff = d2 - d1;
    var daysRemaining = diff / 1000 / 60 / 60 / 24;
    var amc_rate = $('#txtAMCRate').val();
    var num = (amc_rate / 365) * daysRemaining;

    var amc_amount = Math.ceil(num * 10) / 10;
    var amc_saleID = cGSaleID;
    var amc_cusID = cGCusID;
    var insUser = '2712BC15-BABE-4A23-9E3C-A8119695BD38'; //should add cookie value later
    var updUser = '2712BC15-BABE-4A23-9E3C-A8119695BD38'; //should add cookie value later

    var params = {
        'agid': amc_guid,
        'aid': amc_id,
        'ainvid': amc_invoice,
        'ainvDt': amc_invoiceDt,
        'awrnty': amc_warranty,
        'aexpiry': amc_expiry,
        'apmode': amc_pmode,
        'arate': amc_rate,
        'aAmt': amc_amount,
        'aSid': amc_saleID,
        'aCid': amc_cusID,
        'inUsr': insUser,
        'upUsr':updUser
    }
    var containerDIV = $('#addCustomerWizzard');
    var opts = { 'isLoadingGif': true, 'container': containerDIV };
    MakeJAjaxCall("../_services/insertAmc.ashx", params, 'html', fnAMCAddSuccess, '', '', opts);
}
function fnAMCAddSuccess(response) {
    if (response == "y") {
//        var parentDiv = $('#addCustomerWizzard');
//        var moduleName = "../../pages/amcInvoice.aspx";
//        var _params = '&cid=' + cGCusID + '&sid=' + cGSaleID;
//        var opts = { 'onsucess': onUCPreviewLoaded, 'params': _params };
//        loadModuler(moduleName, parentDiv, opts);
        window.location.href = "../../pages/amcInvoice.aspx?cid=" + cGCusID + "&sid=" + cGSaleID;
    }
    else {

    }
}
function onUCPreviewLoaded() {

}
function tablePrint() {
    var display_setting = "toolbar=no,location=no,directories=no,menubar=no,";
    display_setting += "scrollbars=no,width=750, height=600, left=100, top=25";

    var content_innerhtml = document.getElementById("customerAccountPreview").innerHTML;
    var document_print = window.open("", "", display_setting);
    document_print.document.open();
    document_print.document.write('<html><head><title>Print AMC Acknowledge</title></head>');
    document_print.document.write('<body style="font-family:verdana; font-size:11px;" >');
    document_print.document.write(content_innerhtml);
    document_print.document.write('</body></html>');
    document_print.print();
    document_print.document.close();
    return false;
}
function fnLoadNewCustomerForm() {
    $('.placeholderMenuItem_Clicked').removeClass('placeholderMenuItem_Clicked');
    $('#menuNCustomer').attr('class', 'placeholderMenuItem_Clicked');
    window.location.href = "../pages/addCustomer.aspx";
}
function fnFinishClick() {
    //if ($get("isCustomerCopy").checked == true) {}
    $('.placeholderMenuItem_Clicked').removeClass('placeholderMenuItem_Clicked');
    $('#menuNCustomer').attr('class', 'placeholderMenuItem_Clicked');
    window.location.href = "../../pages/customerdetails.aspx?cid=" + getUrlVars()["cid"];
}

function searchByName() {
    //alert("hi");
    var nameSearchTxt="";
    if ($('#txtThmSearchBox').val() != '' && $('#txtThmSearchBox').val() != "Type Customer Name") {
        nameSearchTxt = $('#txtThmSearchBox').val();
        var parentDiv = $('#allCustomerGrid');
        var moduleName = "../../_modules/ucAllCustomers.ascx";
        var _params = '&srchWrd=' + nameSearchTxt;
        var opts = { 'onsucess': onUCPreviewLoaded, 'params': _params };
        loadModuler(moduleName, parentDiv, opts);
    }
}
function searchByMobile() {
    var nameSearchTxt = "";
    if ($('#txtSearchMobile').val() != '' && $('#txtSearchMobile').val() != "Type Mobile Number") {
        nameSearchTxt = $('#txtSearchMobile').val();
        var parentDiv = $('#allCustomerGrid');
        var moduleName = "../../_modules/ucAllCustomers.ascx";
        var _params = '&srchWrd=' + nameSearchTxt;
        var opts = { 'onsucess': onUCPreviewLoaded, 'params': _params };
        loadModuler(moduleName, parentDiv, opts);
    }
}
function searchByEmail() {
    var nameSearchTxt = "";
    if ($('#txtSearchEmail').val() != '' && $('#txtSearchEmail').val() != "Type Email ID") {
        nameSearchTxt = $('#txtSearchEmail').val();
        var parentDiv = $('#allCustomerGrid');
        var moduleName = "../../_modules/ucAllCustomers.ascx";
        var _params = '&srchWrd=' + nameSearchTxt;
        var opts = { 'onsucess': onUCPreviewLoaded, 'params': _params };
        loadModuler(moduleName, parentDiv, opts);
    }
}
function defaultLoad() {
    //alert("hi");
    var parentDiv = $('#allCustomerGrid');
    var moduleName = "../../_modules/ucAllCustomers.ascx";
    var _params = '&srchWrd=';
    var opts = { 'onsucess': onUCPreviewLoaded, 'params': _params };
    loadModuler(moduleName, parentDiv, opts);
}
function searchByCusID() {
    var nameSearchTxt = "";
    if ($('#txtSearchID').val() != '' && $('#txtSearchID').val() != "Type Customer ID") {
        nameSearchTxt = $('#txtSearchID').val();
        var parentDiv = $('#allCustomerGrid');
        var moduleName = "../../_modules/ucAllCustomers.ascx";
        var _params = '&srchWrd=' + nameSearchTxt;
        var opts = { 'onsucess': onUCPreviewLoaded, 'params': _params };
        loadModuler(moduleName, parentDiv, opts);
    }
}